package fina2.dcs.common.xls;

import java.util.Calendar;

public class XlsNameParser {
	private XlsNameParser() {

	}

	public static String getBankCode(String xlsName, String... pattern) {
		String bankCode = "";
		for (int j = 0; j < pattern.length; j++) {
			if ((pattern[j] == null) || (pattern[j].trim().length() == 0)) {
				continue;
			}
			if (xlsName.toLowerCase().trim().matches(pattern[j].toLowerCase().trim())) {
				int startIndex = 0, stopIndex = 0;
				for (int i = 0; i < pattern[j].length(); i++) {
					if (pattern[j].charAt(i) == '[')
						startIndex = i;
					else if (pattern[j].charAt(i) == '{') {
						stopIndex = startIndex + Integer.parseInt(pattern[j].charAt(i + 1) + "");
						break;
					}
				}
				bankCode = xlsName.substring(startIndex, stopIndex);
				break;
			}
		}
		if (bankCode.trim().length() != 0)
			if (bankCode.contains("."))
				bankCode = xlsName.substring(6, 11);
		return bankCode;
	}

	public static int getDay(String xlsName) {
		// SCC123m012011.xls
		String day = "";
		int i = xlsName.lastIndexOf(".");
		if (i != -1) {
			day = xlsName.substring(i - 8, i - 6);
		}

		return Integer.parseInt(day);
	}

	public static String getMonth(String xlsName) {
		// SCC123m012011.xls
		String month = "";
		int i = xlsName.lastIndexOf(".");
		if (i != -1) {
			month = xlsName.substring(i - 6, i - 4);
		}

		return month;
	}

	public static String getYear(String xlsName) {
		String year = "";
		int i = xlsName.lastIndexOf(".");
		if (i != -1) {
			year = xlsName.substring(i - 4, i);
		}
		return year;
	}

	/**
	 * 
	 * @param xlsName
	 * @return symbol which defines one of the following:
	 *         {m=MONTHLY,q=QUARTERLY}
	 */
	public static String getDateType(String xlsName) {
		String type = "m";
		int i = xlsName.lastIndexOf('.');
		if (i != -1) {
			type = xlsName.substring(i - 7, i - 6);
		}
		return type;
	}

	public static String getDateType(String pattern, String xlsName) {
		// DTMFI[0-9]{3}m(01|02|03|04|05|06|07|08|09|10|11|12)[1-9][0-9][0-9][0-9].xls
		// DTMFI104m022011.xls
		String type = "";
		int index = Character.getNumericValue(pattern.charAt(pattern.indexOf('}') - 1));
		int number = pattern.indexOf('[');
		int typeIndex = index + number;
		type = xlsName.charAt(typeIndex) + "";
		return type;
	}

	public static String getPeriodStart(int month, String type, int year) {
		String start = null;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		if (type.equals("q")) {
			c.set(Calendar.MONTH, month + 1);
			// end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" +
			// convertToTwoDigitMonth((c.get(Calendar.MONTH) + 1)) + "/" +
			// c.get(Calendar.YEAR);
			if (month == 1) {
				start = "01/01/" + c.get(Calendar.YEAR);
			} else if (month == 2) {
				start = "01/04/" + c.get(Calendar.YEAR);
			} else if (month == 3) {
				start = "01/07/" + c.get(Calendar.YEAR);
			} else if (month == 4) {
				start = "01/10/" + c.get(Calendar.YEAR);
			} else {
				throw new RuntimeException("Illegal Month for quarterly period");
			}
		} else if (type.equalsIgnoreCase("d") || type.equalsIgnoreCase("j")) {

		} else {
			start = "01/" + convertToTwoDigit(month) + "/" + c.get(Calendar.YEAR);
		}
		return start;
	}

	public static String getPeriodStart(int day, int month, String type, int year) {
		String start = null;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		if (type.equals("q")) {
			c.set(Calendar.MONTH, month + 1);
			// end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" +
			// convertToTwoDigitMonth((c.get(Calendar.MONTH) + 1)) + "/" +
			// c.get(Calendar.YEAR);
			if (month == 1) {
				start = "01/01/" + c.get(Calendar.YEAR);
			} else if (month == 2) {
				start = "01/04/" + c.get(Calendar.YEAR);
			} else if (month == 3) {
				start = "01/07/" + c.get(Calendar.YEAR);
			} else if (month == 4) {
				start = "01/10/" + c.get(Calendar.YEAR);
			} else {
				throw new RuntimeException("Illegal Month for quarterly period");
			}
		} else if (type.equalsIgnoreCase("d") || type.equalsIgnoreCase("j")) {
			start = convertToTwoDigit(day) + "/" + convertToTwoDigit(month) + "/" + year;
		} else {
			start = "01/" + convertToTwoDigit(month) + "/" + c.get(Calendar.YEAR);
		}
		return start;
	}

	/**
	 * 
	 * @param month
	 *            number of month {01-12}
	 * @param type
	 *            defines type of period {m=MONTHLY,q=QUARTERLY}
	 * @param year
	 *            defines year
	 * @return formated string with pattern dd/MM/YYYY
	 */
	public static String getPeriodEnd(int month, String type, int year) {
		String end = null;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		if (type.equals("q")) {
			c.set(Calendar.MONTH, month + 1);
			// end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" +
			// convertToTwoDigitMonth((c.get(Calendar.MONTH) + 1)) + "/" +
			// c.get(Calendar.YEAR);
			if (month == 1) {
				end = "31/03/" + c.get(Calendar.YEAR);
			} else if (month == 2) {
				end = "30/06/" + c.get(Calendar.YEAR);
			} else if (month == 3) {
				end = "30/09/" + c.get(Calendar.YEAR);
			} else if (month == 4) {
				end = "31/12/" + c.get(Calendar.YEAR);
			} else {
				throw new RuntimeException("Illegal Month for quarterly period");
			}
		} else {
			c.set(Calendar.MONTH, month - 1);
			end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" + convertToTwoDigit((c.get(Calendar.MONTH) + 1)) + "/" + c.get(Calendar.YEAR);
		}
		return end;
	}

	/**
	 * 
	 * @param month
	 *            number of month {01-12}
	 * @param type
	 *            defines type of period {m=MONTHLY,q=QUARTERLY}
	 * @param year
	 *            defines year
	 * @return formated string with pattern dd/MM/YYYY
	 */
	public static String getPeriodEnd(int day, int month, String type, int year) {
		String end = null;
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		if (type.equals("q")) {
			c.set(Calendar.MONTH, month + 1);
			// end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" +
			// convertToTwoDigitMonth((c.get(Calendar.MONTH) + 1)) + "/" +
			// c.get(Calendar.YEAR);
			if (month == 1) {
				end = "31/03/" + c.get(Calendar.YEAR);
			} else if (month == 2) {
				end = "30/06/" + c.get(Calendar.YEAR);
			} else if (month == 3) {
				end = "30/09/" + c.get(Calendar.YEAR);
			} else if (month == 4) {
				end = "31/12/" + c.get(Calendar.YEAR);
			} else {
				throw new RuntimeException("Illegal Month for quarterly period");
			}
		} else if (type.equalsIgnoreCase("d") || type.equalsIgnoreCase("j")) {
			end = convertToTwoDigit(day) + "/" + convertToTwoDigit(month) + "/" + year;
		} else {
			c.set(Calendar.MONTH, month - 1);
			end = c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" + convertToTwoDigit((c.get(Calendar.MONTH) + 1)) + "/" + c.get(Calendar.YEAR);
		}
		return end;
	}

	private static String convertToTwoDigit(int month) {
		String s = month + "";
		return (s.length() == 1) ? ("0" + s) : (s);
	}

	public static void main(String[] args) {
		String s = "BNK12345d01012011.xls";
		// System.out.println("Bank Code: "+getBankCode(s,
		String pattern = "BNK[0-9]{5}d[01-31]{2}(01|02|03|04|05|06|07|08|09|10|11|12)[1-9][0-9][0-9][0-9].xls";
		System.out.println(getBankCode(s, pattern));
		System.out.println(getDateType(pattern,s));
		System.out.println(getPeriodStart(getDay(s), Integer.parseInt(getMonth(s)), getDateType(pattern, s), Integer.parseInt(getYear(s))));
		System.out.println(getPeriodEnd(getDay(s), Integer.parseInt(getMonth(s)), getDateType(pattern, s), Integer.parseInt(getYear(s))));
	}
}